﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Windows.Media;
using WPFCABShell.Shell.Interface;
using WPFCABShell.Shell.Interface.Services.Command;
using WPFCABShell.Shell.Interface.Services.StatusBar;
using Microsoft.Practices.ObjectBuilder;
using CAB = Microsoft.Practices.CompositeUI.Commands;
using WPF = System.Windows.Input;

namespace StatusBarDemo
{
	public sealed class StatusBarDemoPresenter : Presenter<IStatusBarDemoView>
	{
		private readonly IStatusBarService _statusBarService;

		public StatusBarDemoPresenter([Dependency] IStatusBarService statusBarService)
		{
			_statusBarService = statusBarService;
		}

		protected override void OnViewSet()
		{
			base.OnViewSet();
			_images = new List<object>(new object[] { View.FirstImage, View.SecondImage, View.ThirdImage });
		}

		#region Example 1

		private IMessageOperation _primaryMessageOperation;
		private IMessageOperation _secondaryMessageOperation;
		private readonly List<string> _messages = new List<string>(new string[] { "Hello", "world", "from", "the", "status", "bar", "service!", "This is the last message." });
		private List<object> _images;

		public bool Example1CanStartPrimaryMessageOperation()
		{
			return _primaryMessageOperation == null;
		}

		public void Example1StartPrimaryMessageOperation()
		{
			_primaryMessageOperation = _statusBarService.BeginMessageOperation();
		}

		public bool Example1CanChangePrimaryMessage()
		{
			return _primaryMessageOperation != null;
		}

		public void Example1ChangePrimaryMessage()
		{
			int currentIndex = _messages.IndexOf(_primaryMessageOperation.Text);
			++currentIndex;

			if (currentIndex == _messages.Count)
			{
				currentIndex = 0;
			}

			_primaryMessageOperation.Text = _messages[currentIndex];
		}

		public bool Example1CanEndPrimaryMessageOperation()
		{
			return _primaryMessageOperation != null;
		}

		public void Example1EndPrimaryMessageOperation()
		{
			_primaryMessageOperation.Dispose();
			_primaryMessageOperation = null;
		}

		public bool Example1CanStartSecondaryMessageOperation()
		{
			return _secondaryMessageOperation == null;
		}

		public void Example1StartSecondaryMessageOperation()
		{
			_secondaryMessageOperation = _statusBarService.BeginSecondaryMessageOperation();
		}

		public bool Example1CanChangeSecondaryMessage()
		{
			return _secondaryMessageOperation != null;
		}

		public void Example1ChangeSecondaryMessage()
		{
			int currentIndex = _messages.IndexOf(_secondaryMessageOperation.Text);
			++currentIndex;

			if (currentIndex == _messages.Count)
			{
				currentIndex = 0;
			}

			_secondaryMessageOperation.Text = _messages[currentIndex];
		}

		public bool Example1CanChangeSecondaryMessageImage()
		{
			return _secondaryMessageOperation != null;
		}

		public void Example1ChangeSecondaryMessageImage()
		{
			int currentIndex = _images.IndexOf(_secondaryMessageOperation.Image);
			++currentIndex;

			if (currentIndex == _images.Count)
			{
				currentIndex = 0;
			}

			_secondaryMessageOperation.Image = _images[currentIndex];
		}

		public bool Example1CanEndSecondaryMessageOperation()
		{
			return _secondaryMessageOperation != null;
		}

		public void Example1EndSecondaryMessageOperation()
		{
			_secondaryMessageOperation.Dispose();
			_secondaryMessageOperation = null;
		}

		#endregion

		#region Example 2

		private IProgressOperation _determinateProgressOperation;
		private IProgressOperation _indeterminateProgressOperation;

		public bool Example2CanStartDeterminateProgressOperation()
		{
			return _determinateProgressOperation == null;
		}

		public void Example2StartDeterminateProgressOperation()
		{
			_determinateProgressOperation = _statusBarService.BeginProgressOperation();
			_determinateProgressOperation.Text = "Demonstrating...";
			_determinateProgressOperation.Minimum = 0;
			_determinateProgressOperation.Maximum = 10;
			_determinateProgressOperation.Value = 0;
		}

		public bool Example2CanIncreaseProgress()
		{
			return _determinateProgressOperation != null && _determinateProgressOperation.Value != 10;
		}

		public void Example2IncreaseProgress()
		{
			++_determinateProgressOperation.Value;
		}

		public bool Example2CanEndDeterminateProgressOperation()
		{
			return _determinateProgressOperation != null;
		}

		public void Example2EndDeterminateProgressOperation()
		{
			_determinateProgressOperation.Dispose();
			_determinateProgressOperation = null;
		}

		public bool Example2CanStartIndeterminateProgressOperation()
		{
			return _indeterminateProgressOperation == null;
		}

		public void Example2StartIndeterminateProgressOperation()
		{
			_indeterminateProgressOperation = _statusBarService.BeginProgressOperation();
			_indeterminateProgressOperation.Text = "Demonstrating...";
			_indeterminateProgressOperation.IsIndeterminate = true;
		}

		public bool Example2CanEndIndeterminateProgressOperation()
		{
			return _indeterminateProgressOperation != null;
		}

		public void Example2EndIndeterminateProgressOperation()
		{
			_indeterminateProgressOperation.Dispose();
			_indeterminateProgressOperation = null;
		}

		#endregion

		#region Example 3

		private IMessageOperation _lowPriorityOperation;
		private IMessageOperation _normalPriorityOperation;
		private IMessageOperation _highPriorityOperation;

		public bool Example3CanStartLowPriorityOperation()
		{
			return _lowPriorityOperation == null;
		}

		public void Example3StartLowPriorityOperation()
		{
			_lowPriorityOperation = _statusBarService.BeginMessageOperation(StatusPriority.Low);
			_lowPriorityOperation.Text = "Low priority message";
		}

		public bool Example3CanStartNormalPriorityOperation()
		{
			return _normalPriorityOperation == null;
		}

		public void Example3StartNormalPriorityOperation()
		{
			_normalPriorityOperation = _statusBarService.BeginMessageOperation();
			_normalPriorityOperation.Text = "Normal priority message";
		}

		public bool Example3CanStartHighPriorityOperation()
		{
			return _highPriorityOperation == null;
		}

		public void Example3StartHighPriorityOperation()
		{
			_highPriorityOperation = _statusBarService.BeginMessageOperation(StatusPriority.High);
			_highPriorityOperation.Text = "High priority message";
		}

		public bool Example3CanEndHighPriorityOperation()
		{
			return _highPriorityOperation != null;
		}

		public void Example3EndHighPriorityOperation()
		{
			_highPriorityOperation.Dispose();
			_highPriorityOperation = null;
		}

		public bool Example3CanEndNormalPriorityOperation()
		{
			return _normalPriorityOperation != null;
		}

		public void Example3EndNormalPriorityOperation()
		{
			_normalPriorityOperation.Dispose();
			_normalPriorityOperation = null;
		}

		public bool Example3CanEndLowPriorityOperation()
		{
			return _lowPriorityOperation != null;
		}

		public void Example3EndLowPriorityOperation()
		{
			_lowPriorityOperation.Dispose();
			_lowPriorityOperation = null;
		}

		#endregion
	}
}
